package cn.xmkeshe.front.impl;
import cn.xmkeshe.front.IMailServiceFront;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;

import org.thymeleaf.*;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

@Service
public class IMailServiceFrontImpl implements IMailServiceFront {

  @Value("${spring.mail.username}")
  private String mailUsername;

  @Resource
  private JavaMailSender javaMailSender;
  @Resource
  private TemplateEngine templateEngine;


  public void sendMailActivationAccount(String activationUrl,String email){

    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try{
      MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true);
//      设置邮件主题
      message.setSubject("欢迎来到珠科物流-个人账号激活");
//      设置邮件发送者
      message.setFrom(mailUsername);
//      设置邮件接收者
      message.setTo(email);
//      设置邮件的发送日期
      message.setSentDate(new Date());
//      创建上下文环境
      Context context = new Context();
      context.setVariable("activationUrl",activationUrl);
      String text = templateEngine.process("active-account.html",context);
//      邮件发送
      message.setText(text,true);
    }catch (MessagingException e){
      e.printStackTrace();
    }
    javaMailSender.send(mimeMessage);
  }
}
